package gui;

import dados.Aluno;
import dados.BancoQuestoes;
import dados.PastaCorrecao;
import gui.modelos.KeyListenerJanela;
import java.io.File;
import java.io.IOException;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import logica.Arquivos;
import logica.Utilitarios;

public class PastaCorrigida extends javax.swing.JPanel {

	private Aluno[] alunos = null;
    
    /** Creates new form PainelLista */
    public PastaCorrigida() {
    	System.out.println("gui.PastaCorrigida");
        initComponents();
        iniciarCampos();
        this.addKeyListener(new KeyListenerJanela());
    }
    
    private void iniciarCampos() {
        int nroQuestoes = BancoQuestoes.getInstancia().getTamanhoBanco();
        String[] vetorQuestao = new String[nroQuestoes];
        
        for (int i = 0; i <= nroQuestoes - 1; i++) {
            vetorQuestao[i] = "Questão " + (i + 1);
        }
        
        DefaultComboBoxModel modelQuestao = new DefaultComboBoxModel(vetorQuestao);
        cmbQuestao.setModel(modelQuestao);
        cmbQuestao.setSelectedIndex(0);
        alunos = Utilitarios.encontrarAlunos(0);
        String[] nomeAlunos = new String[alunos.length];
        int i = 0;
        for (Aluno aluno : alunos) {
            nomeAlunos[i] = alunos[i].getDiretorioAluno().getName();
            i++;
        }
        listaAlunos.setListData(nomeAlunos);
        if (alunos.length > 0) {
            listaAlunos.setSelectedIndex(0);
        }   
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" C�digo Gerado ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jSplitPane = new javax.swing.JSplitPane();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane = new javax.swing.JScrollPane();
        txtFonte = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        cmbQuestao = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaAlunos = new javax.swing.JList();

        jSplitPane.setDividerLocation(120);
        jSplitPane.setDividerSize(10);
        jPanel3.setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Código Fonte do Aluno");
        jPanel3.add(jLabel1, new java.awt.GridBagConstraints());

        txtFonte.setColumns(20);
        txtFonte.setRows(5);
        jScrollPane.setViewportView(txtFonte);

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
            .add(jScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE))
        );
        jSplitPane.setRightComponent(jPanel2);

        cmbQuestao.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbQuestaoItemStateChanged(evt);
            }
        });

        listaAlunos.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listaAlunosValueChanged(evt);
            }
        });

        jScrollPane1.setViewportView(listaAlunos);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(cmbQuestao, 0, 119, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(cmbQuestao, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE))
        );
        jSplitPane.setLeftComponent(jPanel1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jSplitPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 482, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(11, 11, 11)
                .add(jSplitPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void listaAlunosValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listaAlunosValueChanged
        int alunoSelecionado = listaAlunos.getSelectedIndex();
        if(alunoSelecionado < 0) {
            return;
        }
        int questaoSelecionada = cmbQuestao.getSelectedIndex();
        File fonteAluno = alunos[alunoSelecionado].getFontes()[questaoSelecionada].getArquivo();
        try {
            String textoArquivo = Arquivos.getTextoArquivo(fonteAluno);
            txtFonte.setText(textoArquivo);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "O arquivo não pôde ser encontrado, " +
                    "por favor verifique\n se ocorreu alguma violação nos dados da " +
                    "correção.", "Arquivo não encontrado!", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_listaAlunosValueChanged
  
    private void cmbQuestaoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbQuestaoItemStateChanged
        listaAlunos.setListData(new Object[0]);
        txtFonte.setText("");
        int indiceQuestao = cmbQuestao.getSelectedIndex();
        alunos = Utilitarios.encontrarAlunos(indiceQuestao);
        String[] nomeAlunos = new String[alunos.length];
        int i = 0;
        for (Aluno aluno : alunos) {
            nomeAlunos[i] = alunos[i].getDiretorioAluno().getName();
            i++;
        }
        listaAlunos.setListData(nomeAlunos);
        if (alunos.length > 0) {
            listaAlunos.setSelectedIndex(0);
        }
    }//GEN-LAST:event_cmbQuestaoItemStateChanged
    
    // Declara��o de vari�veis - n�o modifique//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbQuestao;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane;
    private javax.swing.JList listaAlunos;
    private javax.swing.JTextArea txtFonte;
    // Fim da declara��o de vari�veis//GEN-END:variables
}
